package _12对象流案例;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *需求：将存有多个自定义对象的集合保存到 list.txt 文件中，并读取list.txt 。
 *
 */
public class Demo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        List<Student> students =new ArrayList<>();

        for (int i = 1; i <= 10; i++) {
            students.add(new Student(("学生"+i),(i%2==0?"男":"女"),22));
        }

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day11\\abd\\list.txt"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day11\\abd\\list.txt"));

        for (Student student : students) {
            oos.writeObject(student);
        }
        oos.writeObject(null);//序列化完成后，添加null标记即可方便遍历

        Object o;
        while ((o=ois.readObject())!=null){
            System.out.println(o);
        }

        oos.close();
    }
}
